#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import inspect
import random
from functools import wraps

"""
1. 随机生成一个字符串：
    *. 字符串是由 数字和大小写字母组成
    *. 不含特殊符号
    *. 可以指定生成几位

1. 给定一个只包含大小写字母，数字的字符串，对其进行排序，保证：
    *. s = "Sorting1234"
    *. 所有的小写字母在大写字母前面
    *. 所有的字母在数字前面
    *. 所有的奇数在偶数前面
"""


def parameter_checking(fn):
    """Check function parameter"""

    @wraps(fn)
    def wrapper(*args, **kwargs):
        sig = inspect.signature(fn)
        for index, (k, v) in enumerate(sig.parameters.items()):
            data = kwargs.get(k, None)
            try:
                data = data if data else args[index]
            except IndexError:
                data = v.default
            if not isinstance(data, v.annotation):
                raise TypeError('Type not match')
        return fn(*args, **kwargs)

    return wrapper


@parameter_checking
def random_string(n: int = 8) -> str:
    """Generate a random string with char and digit

    :param n: length of sting
    :return: random string
    """
    char_digits = ('abcdefghijklmnopqrstuvwxyz' +
                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
                   '0123456789')
    # 想想有没有可能会出现 8个一样的字符
    return ''.join([random.choice(char_digits) for _ in range(n)])


@parameter_checking
def specific_sorting(strings: str) -> str:
    """As requires sorting, lower letter > upper letter > odd digit > even digit

    :param strings: sorted target string
    :return: sorting string
    """
    strings_lst = list(strings)
    # [create multi empty list](https://hant-kb.kutu66.com/others/post_13509412)
    # avoid track a, b, c = tuple([[]] * 3)
    # ```
    # In [64]: a, b, c = tuple(([[]] * 3))
    # In [65]: a.append(100)
    # In [66]: a, b, c
    # Out[66]: ([100], [100], [100])
    # ```
    upper_chars, lower_chars, odd_digits, even_digits = ([] for _ in range(4))

    for s in strings_lst:
        if s.isalpha():
            _ = upper_chars.append(s) if s.isupper() else lower_chars.append(s)
        elif s.isdigit():
            _ = odd_digits.append(s) if int(
                s) % 2 != 0 else even_digits.append(s)

    res = [sorted(r) for r in
           (lower_chars, upper_chars, odd_digits, even_digits)]
    # [How to make a flat list](https://stackoverflow.com/a/952946/8057310)
    return ''.join(sum(res, []))


rs = random_string(2)
print(rs, specific_sorting(rs))
